<?php

namespace SisesporteAdmin\Controller;

use SisesporteAdmin\Form\ProfessorForm;
use Zend\View\Model\ViewModel;
use Sisesporte\Utils\Utils;

class ProfessorController extends CrudController {

    protected $utils;
    protected $msg;

    public function __construct() {
        $this->entity = "Sisesporte\Entity\Professor";
        $this->form = "SisesporteAdmin\Form\ProfessorForm";
        $this->service = "Sisesporte\Service\ProfessorService";
        $this->controller = "professor";
        $this->route = "sisesporte-admin";
        $this->utils = new Utils();
    }

    public function newAction() {
        $form = new ProfessorForm($this->getEm());
        $request = $this->getRequest();
        $error = false;

        if ($request->isPost()) {

            $post = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray()
            );

            $filter = new \Zend\Filter\File\RenameUpload($_SERVER['DOCUMENT_ROOT'] . "upload/foto");
            $filter->setUseUploadName(true);
            $filter->filter($post['noImagem']);
            $service = $this->getServiceLocator()->get($this->service);
            
            if(count($service->findProfessorByCPF($post['noCref'])) == 0)
            {
                $service->insert($post);
                $this->msg = true;
            } else {
                $this->msg = false;
                $error = 'Registro de CREF já existente, informe um número novo.';
            }

        }

        return new ViewModel(array('form' => $form, 'msg' => $this->msg, 'error' => $error));
    }

    public function editAction() {
        $form = new ProfessorForm($this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
        }

        if ($request->isPost()) {
            $form->setData($request->getPost());
            $service = $this->getServiceLocator()->get($this->service);
            $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());
            $this->msg = TRUE;
//              return $this->redirect()->toRoute('sisesporte-admin-interna', array('controller' => $this->controller, 'action' => 'view', 'id' => $entity->getSqPessoa()));
        }

        return new ViewModel(array('form' => $form,
                    'entity' => $entity->getSqPessoa(),
                    'msg' => $this->msg)
        );
    }

    public function viewAction() {
        $form = new ProfessorForm($this->getEm());

        $repository = $this->getEm()->getRepository("Sisesporte\Entity\Pessoa");
        $entity = $repository->find($this->params()->fromRoute('id', 0));
        if ($this->params()->fromRoute('id', 0)) {
            $form->setDefaultsFromEntity($entity);
            $tipoAfastamento = $this->getEm()
                    ->getRepository('Sisesporte\Entity\TipoAfastamento')
                    ->findAll();
            $modalidades = $this->getEm()
                    ->getRepository('Sisesporte\Entity\ModalidadeProfessor')
                    ->findBy(array('sqPessoa' => $this->params()->fromRoute('id', 0)));
        }

        return new ViewModel(
                        array(
                            'form' => $form,
                            'entity' => $entity->getSqPessoa(),
                            'msg' => $this->msg,
                            'tipoAfastamento' => $tipoAfastamento,
                            'modalidades' => $modalidades,
                            'utils' => $this->utils
                        )
        );
    }

}
